﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Admin_survey_question : System.Web.UI.Page
{
    myDataSetTableAdapters.SurveysTableAdapter surAdapter = new myDataSetTableAdapters.SurveysTableAdapter();
    myDataSetTableAdapters.SurveyCategoryTableAdapter surCateAdapter = new myDataSetTableAdapters.SurveyCategoryTableAdapter();
    myDataSetTableAdapters.Survey_QuestionTableAdapter surQuesAdapter = new myDataSetTableAdapters.Survey_QuestionTableAdapter();
    myDataSetTableAdapters.QuestionsTableAdapter quesAdapter = new myDataSetTableAdapters.QuestionsTableAdapter();
    myDataSetTableAdapters.AnswersTableAdapter answerAdapter = new myDataSetTableAdapters.AnswersTableAdapter();
    protected void Page_Load(object sender, EventArgs e)
    {
        Panel2.Visible = true;
        Panel1.Visible = false;
        Panel3.Visible = false;
        Panel4.Visible = false;
        Panel5.Visible = false;
        lblSurTitle.Visible = false;
        cmdChooseSur.Visible = false;
        lblInfo.Visible = true;
        displaySur();
        displayAllQues();
        myDataSet.SurveyCategoryDataTable surCateTable = surCateAdapter.GetData();
        DropDownList1.AppendDataBoundItems = true;
        DropDownList1.DataSource = surCateTable.DefaultView;
        DropDownList1.DataTextField = "name";
        DropDownList1.DataValueField = "id";
        DropDownList1.DataBind();
    }

    protected void displaySur()
    {
        myDataSet.SurveysDataTable surTable = surAdapter.GetDataConvert();
        GridView1.DataSource = surTable;
        GridView1.DataBind();
    }

    protected void displayAllQues()
    {
        myDataSet.QuestionsDataTable quesTable = quesAdapter.GetDataConvert();
        GridView2.DataSource = quesTable;
        GridView2.DataBind();
    }

    protected void displayQuesBySur(int id)
    {
        myDataSet.QuestionsDataTable quesTable = quesAdapter.GetDataConvertBySurId(id);
        GridView3.DataSource = quesTable;
        GridView3.DataBind();
    }

    protected void clearSur()
    {
        txtId.Text = "";
        txtTitle.Text = "";
        txtDes.Text = "";
        DropDownList1.SelectedValue = "-1";
        RadioButtonList1.SelectedIndex = -1;
        RadDatePicker1.SelectedDate = null;
    }

    protected void cmdChooseSur_Click(object sender, EventArgs e)
    {
        displaySur();
        Panel1.Visible = false;
        Panel2.Visible = true;
        Panel3.Visible = false;
        Panel4.Visible = false;
        Panel5.Visible = false;
        lblSurTitle.Visible = false;
        cmdChooseSur.Visible = false;
        lblInfo.Visible = true;
        clearSur();
    }

    protected void getDataSurById(int id)
    {
        myDataSet.SurveysDataTable surTable = surAdapter.GetDataById(id);
        txtId.Text = surTable[0]["id"].ToString();
        txtTitle.Text = surTable[0]["title"].ToString();
        txtDes.Text = surTable[0]["description"].ToString();
        DropDownList1.SelectedValue = surTable[0]["category_id"].ToString();
        RadioButtonList1.SelectedValue = surTable[0]["is_active"].ToString();
        RadDatePicker1.SelectedDate = DateTime.Parse(surTable.Rows[0]["created_at"].ToString());
    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        int id = int.Parse(GridView1.DataKeys[e.NewEditIndex].Value.ToString());
        Panel1.Visible = true;
        Panel2.Visible = true;
        Panel3.Visible = false;
        Panel4.Visible = false;
        Panel5.Visible = false;
        lblSurTitle.Visible = false;
        cmdChooseSur.Visible = false;
        lblInfo.Visible = false;
        getDataSurById(id);
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        displaySur();
    }


    protected void cmdChoose1_Click(object sender, EventArgs e)
    {
        Panel1.Visible = false;
        Panel2.Visible = false;
        Panel3.Visible = false;
        Panel4.Visible = true;
        Panel5.Visible = true;
        lblSurTitle.Visible = true;
        cmdChooseSur.Visible = true;
        lblInfo.Visible = false;
        lblSurTitle.Text = txtTitle.Text;
        txtSurId.Text = txtId.Text;
        displayQuesBySur(int.Parse(txtSurId.Text.Trim()));
    }

    protected void cmdHide1_Click(object sender, EventArgs e)
    {
        displaySur();
        Panel1.Visible = false;
        Panel2.Visible = true;
        Panel3.Visible = false;
        Panel4.Visible = false;
        Panel5.Visible = false;
        lblSurTitle.Visible = false;
        cmdChooseSur.Visible = false;
        lblInfo.Visible = true;
    }

    protected void getDataQuesById(int id)
    {
        myDataSet.QuestionsDataTable quesTable = quesAdapter.GetDataById(id);
        txtQuesId.Text = quesTable.Rows[0]["id"].ToString();
        lblQuestionAnswer.Text = quesTable.Rows[0]["question"].ToString();
        myDataSet.AnswersDataTable answerTable = answerAdapter.GetDataByQuesId(int.Parse(txtQuesId.Text.Trim()));
        DataList1.DataSource = answerTable;
        DataList1.DataBind();
    }

    protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
    {
        int id = int.Parse(GridView2.DataKeys[e.NewEditIndex].Value.ToString());
        Panel1.Visible = false;
        Panel2.Visible = false;
        Panel3.Visible = true;
        Panel4.Visible = true;
        Panel5.Visible = true;
        lblSurTitle.Visible = true;
        cmdChooseSur.Visible = true;
        lblInfo.Visible = false;
        getDataQuesById(id);
    }

    protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView2.PageIndex = e.NewPageIndex;
        displayAllQues();
    }

    protected void cmdChoose2_Click(object sender, EventArgs e)
    {
        try
        {
            Panel1.Visible = false;
            Panel2.Visible = false;
            Panel3.Visible = false;
            Panel4.Visible = true;
            Panel5.Visible = true;
            lblSurTitle.Visible = true;
            cmdChooseSur.Visible = true;
            lblInfo.Visible = false;
            surQuesAdapter.InsertQuery(int.Parse(txtSurId.Text.Trim()), int.Parse(txtQuesId.Text.Trim()));
        }
        catch
        {
            RegisterStartupScript("as", "<script>alert('This question exists in this survey')</script>");
        }
        
        displayQuesBySur(int.Parse(txtSurId.Text.Trim()));
    }

    protected void cmdHide2_Click(object sender, EventArgs e)
    {
        displayQuesBySur(int.Parse(txtSurId.Text.Trim()));
        Panel1.Visible = false;
        Panel2.Visible = false;
        Panel3.Visible = false;
        Panel4.Visible = true;
        Panel5.Visible = true;
        lblSurTitle.Visible = true;
        cmdChooseSur.Visible = true;
        lblInfo.Visible = false;
    }

    protected void GridView3_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int SurId = int.Parse(txtSurId.Text.Trim());
        int QuesId = int.Parse(GridView3.DataKeys[e.RowIndex].Value.ToString());
        surQuesAdapter.DeleteQuery(SurId, QuesId);
        displayQuesBySur(int.Parse(txtSurId.Text.Trim()));
    }
   
    protected void GridView3_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView3.PageIndex = e.NewPageIndex;
        displayQuesBySur(int.Parse(txtSurId.Text.Trim()));
    }
}
